package com.ctg.itrdc.k8sconsole.service.impl;

import java.net.URL;
import java.text.MessageFormat;

import javax.annotation.Resource;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;

import org.springframework.stereotype.Component;

import com.ctg.itrdc.imageconsole.common.util.HttpRequestUtil;
import com.ctg.itrdc.imageconsole.data.dao.IClusterConfigDao;
import com.ctg.itrdc.imageconsole.data.entity.ClusterConfig;
import com.ctg.itrdc.k8sconsole.service.IReplicasetService;
import com.ctg.itrdc.paspsdk.common.utils.file.PropertiesUtil;

/**
 * 
 * @Description 
 * @author wupiao
 * @since 2017年3月29日
 */
@Component("replicasetService")
public class ReplicasetServiceImpl implements IReplicasetService {


	@Resource
	private IClusterConfigDao clusterConfigDao;

	@Override
	public String getReplicasets(String clusterCode, String name, String perpage, String page) throws Exception {
		ClusterConfig clusterConfig = clusterConfigDao.queryByClusterCode(clusterCode);

		String urlString = PropertiesUtil.getConfigValueCache("k8s.properties", "getReplicasets.api");
		urlString = MessageFormat.format(urlString, clusterConfig.getMonitorUrl(), clusterCode);
		URL url = new URL(urlString);
		HttpRequestUtil requestUtil = new HttpRequestUtil(url);
		WebTarget target = HttpRequestUtil.target;
		Response response = target.queryParam("perpage", perpage).queryParam("page", page).queryParam("name", name).request().get();

		return requestUtil.getResponseJsonData(response);
	}
	
	
	@Override
	public String getReplicasetsByNamesp(String clusterCode, String namesp, String name, String perpage, String page) throws Exception {
		ClusterConfig clusterConfig = clusterConfigDao.queryByClusterCode(clusterCode);

		String urlString = PropertiesUtil.getConfigValueCache("k8s.properties", "getReplicasetsByNamesp.api");
		urlString = MessageFormat.format(urlString, clusterConfig.getMonitorUrl(), clusterCode, namesp);
		URL url = new URL(urlString);
		HttpRequestUtil requestUtil = new HttpRequestUtil(url);
		WebTarget target = HttpRequestUtil.target;
		Response response = target.queryParam("perpage", perpage).queryParam("page", page).queryParam("name", name).request().get();

		return requestUtil.getResponseJsonData(response);
	}
	

	@Override
	public String getReplicasetDetail(String clusterCode, String namesp, String name) throws Exception {
		ClusterConfig clusterConfig = clusterConfigDao.queryByClusterCode(clusterCode);

		String urlString = PropertiesUtil.getConfigValueCache("k8s.properties", "getReplicasetDetail.api");
		urlString = MessageFormat.format(urlString, clusterConfig.getMonitorUrl(), clusterCode, namesp, name);
		URL url = new URL(urlString);
		HttpRequestUtil requestUtil = new HttpRequestUtil(url);
		WebTarget target = HttpRequestUtil.target;
		Response response = target.request().get();

		return requestUtil.getResponseJsonData(response);
	}




}
